﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Grocery
{
    abstract class Customer
    {
        internal Customer(int number, int arrive, int items)
        {
            Number = number;
            Offset = arrive;
            Items = items;
            TotalMinute = 1 * Items;
        }

        internal int Number
        {
            private set;
            get;
        }

        internal int Offset
        {
            private set;
            get;
        }

        abstract internal CustomerType customerType
        {
            get;
        }

        public bool Finished 
        {
            get
            {
                return Math.Abs(TotalMinute) <= 1E-5;
            }
        }

        private float TotalMinute
        {
            get;
            set;
        }

        internal int Items
        {
            get;
            private set;
        }

        internal void Service(float p)
        {
            TotalMinute -= p;
        }

        abstract protected CashierQueue SelectQueue(IEnumerable<CashierQueue> queues);

        internal bool AppendToQueue(int minute, IEnumerable<CashierQueue> queues)
        {
            CashierQueue queue = SelectQueue(queues);
            if (queue != null)
            {
                queue.AppendCustomer(this);
                DebugInfo.Ouput("t=" + minute.ToString()
                    + ", Customer #" + Number.ToString()
                    +" type: " + customerType.ToString()
                    + " with items: " + Items.ToString() 
                    + " go to queue: " + queue.Number.ToString());
                return true;
            }
            return false;
        }
    }
}
